package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.BranchForm;
import com.vcaresolution.logistic.to.DepartmentTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>BranchController</code> is used to track all Branch related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class BranchController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.BRANCHFORM_MODELATTRIBUTE)
	public BranchForm createForm(){
		return new BranchForm();
	}
	
	private void loadRequiredDetailInModelObject(ModelAndView modelAndView){
		modelAndView.addObject(ApplicationConstants.BRANCHCITYLIST_MODELATTRIBUTE, getServiceRegistry().getCityService().findByAll());
	}
	
	/**
	 * This method is called when the GET request has been made to the AddBranch Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_BRANCH_URL,method = RequestMethod.GET)
	public ModelAndView loadAddBranch(@RequestParam(value="companyId") String companyId) {
		ModelAndView modelAndView = new ModelAndView();
		loadRequiredDetailInModelObject(modelAndView);
		modelAndView.addObject("companyId",companyId);
		modelAndView.setViewName(ApplicationConstants.ADD_BRANCH_VIEW);
		modelAndView.addObject(ApplicationConstants.BRANCHFORM_MODELATTRIBUTE, new BranchForm());
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the AddBranch Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_BRANCH_URL,method = RequestMethod.POST)
	public ModelAndView processAddBranch(@Valid BranchForm branchForm, final BindingResult result,final HttpServletRequest request,
											final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		if( !result.hasErrors() ){
			getServiceRegistry().getBranchService().add(branchForm);

			redirectAttrs.addFlashAttribute(ApplicationConstants.SUCCESS_MESSAGE,getMessageSource().getMessage("branchForm.added.message", null, null));
			modelAndView.setView(new RedirectView(ApplicationConstants.EDIT_COMPANY_URL+"?companyId="+branchForm.getCompanyId(), true, true, false));
		}else {
			loadRequiredDetailInModelObject(modelAndView);
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ListBranch Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_BRANCH_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.setViewName(ApplicationConstants.LIST_BRANCH_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the EditBranch Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_BRANCH_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditBranch(@ModelAttribute(ApplicationConstants.BRANCHFORM_MODELATTRIBUTE)
											final BranchForm BranchForm , @RequestParam(value="branchid") String branchid,
											@RequestParam(value="companyId") String companyId,final BindingResult result, 
											final Model model) {
		
		ModelAndView modelAndView = new ModelAndView();
		
		Object branchObject = getServiceRegistry().getBranchService().findById(Integer.parseInt(branchid));
		
		/**editedByVishal*/
		List<DepartmentTO> associatedDepartmentList = getServiceRegistry().getBranchService().getAssociatedDepartmentList(branchid);
//		List<DepartmentTO> associatedDepartmentList = getServiceRegistry().getBranchService().getAssociatedDepartmentList(branchid,DepartmentTO.class);
		
		loadRequiredDetailInModelObject(modelAndView);
		modelAndView.addObject("companyId",companyId);
		modelAndView.addObject(ApplicationConstants.BRANCHFORM_MODELATTRIBUTE, branchObject);
		modelAndView.addObject(ApplicationConstants.DEPARTMENTLIST_MODELATTRIBUTE, associatedDepartmentList);
		modelAndView.setViewName(ApplicationConstants.EDIT_BRANCH_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the EditBranch Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_BRANCH_URL,method = RequestMethod.POST)
	public ModelAndView processEditCompany(@Valid @ModelAttribute(ApplicationConstants.BRANCHFORM_MODELATTRIBUTE) BranchForm branchForm, 
											final BindingResult result,final HttpServletRequest request,
											final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		if( !result.hasErrors() ){
			getServiceRegistry().getBranchService().update(branchForm);

			redirectAttrs.addFlashAttribute(ApplicationConstants.SUCCESS_MESSAGE,getMessageSource().getMessage("branchForm.updated.message", null, null));
			modelAndView.setView(new RedirectView(ApplicationConstants.EDIT_COMPANY_URL+"?companyId="+branchForm.getCompanyId(), true, true, false));
		}else {
			
			String branchId = null;
			if(branchForm.getBranchid() != null && branchForm.getBranchid().intValue() > 0){
				branchId = String.valueOf(branchForm.getBranchid());
			}
			/**editedByVishal*/
			List<DepartmentTO> associatedDepartmentList = getServiceRegistry().getBranchService().getAssociatedDepartmentList(branchId);
//			List<DepartmentTO> associatedDepartmentList = getServiceRegistry().getBranchService().getAssociatedDepartmentList(branchId,DepartmentTO.class);
			
			loadRequiredDetailInModelObject(modelAndView);
			modelAndView.addObject(ApplicationConstants.BRANCHFORM_MODELATTRIBUTE, branchForm);
			modelAndView.addObject(ApplicationConstants.DEPARTMENTLIST_MODELATTRIBUTE, associatedDepartmentList);
			modelAndView.setViewName(ApplicationConstants.EDIT_BRANCH_VIEW);
		}
		
		return modelAndView;
	}
}
